<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

	public function __construct() {
		parent::__construct();
		
		session_start();
		include './config.inc.php';
		include './uc_client/client.php';

	}

		/**
	 * [passLogin 建立session，建立cookie等]
	 * @param  [type] $passid [description]
	 * @return [type] $isRember        [1: 建立cookie, 0: 不建立cookie]
	 */
	protected function passLogin($member, $isRember = 0) {

		$_SESSION['passid'] = $member["id"];
		$_SESSION['stuID'] = $member["idnumber"];
		$_SESSION['adminid'] = $member['adminid'];

		$ip = get_client_ip();
		$this->member->insert_log($member["id"], $ip);

		if ($isRember) {
			//TDO:建立cookie
			$cookie = array(
				'name'   => 'rd_passport',
				'value'  => authcode($member["id"]."_".$member["idnumber"], "ENCODE", $this->config->item("password_encode_key")),
				'expire' => $this->config->item("cookie_time"),
				'domain' => '.redhome.cc',
				'path'   => '/',
				'prefix' => 'rdPassport_',
				'secure' => TRUE
				);
			$this->input->set_cookie($cookie);
		}
	}

	/**
	 * 处理角色登陆或者创建等业务
	 */ 
	protected function roleLogin($passid) {
		if (empty($passid)) {
			show_error("passid is NULL.the controller is roleLogin");
		}
		
		$defaultRole = $this->member->get_default_role($passid);

		if (empty($defaultRole)) {
			if($this->member->get_roles_num($passid) == 1) {  
				$this->member->set_default_role($passid, 0, 1);
			} 
			if ($this->member->get_roles_num($passid) > 0) {
				redirect(site_url("/home/roles"));
				return;
			} 
			//$times = $this->member->get_roles_num($passid);
			//var_dump($times);
			
			//跳转到创建角色页面
			redirect(site_url('/home/createrole'));
			//echo base_url('home/createrole');
		} else {
			$synLoginCode = uc_user_synlogin($defaultRole["uc_id"]);
			$goto = urldecode($this->input->post('goto'));
			if (empty($goto)) {
				$goto = urldecode($this->input->get('goto'));
			}
			if (empty($goto)) {
				if (isset($_SERVER['HTTP_REFERER'])){
					$pattern = "/^".format_preg_string($this->config->item("bbs_path")).".*$/i";
					if (preg_match($pattern, $_SERVER['HTTP_REFERER'])) {
						$goto = $_SERVER['HTTP_REFERER'];
					}
				}
			}
			if (!empty($goto)) {
				//$pattern = "/^".format_preg_string($this->config->item("base_url")).".*$/i";
				$pattern = array(
				"/^".format_preg_string($this->config->item("bbs_path")).".*$/i",
				"/^".format_preg_string($this->config->item("base_url")).".*$/i");

				foreach ($pattern as $key => $value) {
					if (!preg_match($value, $goto)) {
						$match[] = 0;
					} else {
						$match[] = 1;
					}
				}
				$r = 0;
				foreach ($match as $key => $value) {
					$r += $value;
				}
				
				if ($r == 0) {
					log_message("error", "网址来源错误".$goto);
					show_message('登录成功'.$synLoginCode, $this->config->item('bbs_path'));
					return;
				}

				show_message('登录成功'.$synLoginCode, $goto);
				return;
			}
			
			show_message('登录成功'.$synLoginCode, $this->config->item('bbs_path'));
			//echo '需要跳转到角色登陆';
		}
	}
}